package com.abigdreamer.im.client.module.talk.file;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Date;

import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.abigdreamer.im.client.module.network.socket.ClientFileSocket;
import com.abigdreamer.im.client.resourse.ResourcesManagement;
import com.abigdreamer.im.client.util.EIMLogger;
import com.abigdreamer.im.message.FileRequestMessage;
import com.abigdreamer.im.message.FileResponseMessage;
import com.abigdreamer.im.message.MessageType;
import com.abigdreamer.swing.component.JCLabel;

public class SendGetFileNode extends JPanel {

	private static final long serialVersionUID = 1L;
	
	Box box = Box.createVerticalBox();
	JProgressBar fileTransProcess;
	FileRequestMessage message;
	JLabel saveButton;
	JLabel refusebutton;
	JLabel ompletionLabel;
	JLabel speedLabel;
	Box fileInfoBox;
	Box fileGetInfoBox;
	Box userActionbox;
	SendGetFilePanle container;
	boolean isGet;
	private JCLabel lblSpend;
	
	public void hideActionBox() {
		userActionbox.setVisible(false);
	}

	public SendGetFileNode(boolean isGet, FileRequestMessage msg) {
		message = msg;
		this.isGet = isGet;
		fileInfoBox = this.getFileInfoBox(message.getFileName());
		userActionbox = this.getUserAction(isGet);
		box.add(fileInfoBox);
		
		fileTransProcess = new JProgressBar();
		fileTransProcess.setBackground(Color.white);
		fileTransProcess.setForeground(Color.black);
		
		box.add(fileTransProcess);
		box.add(lblSpend = new JCLabel(""));
		box.add(userActionbox);
		this.setBackground(Color.white);
		this.add(box, BorderLayout.CENTER);
		saveButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				saveAction();
			}
		});
		refusebutton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent evt) {
				refuseAction();
			}
		});
	}

	private void saveAction() {
		// -----------操作界面--------------
		userActionbox.setVisible(false);
		fileGetInfoBox = this.getFileGetInfoBox();
		fileGetInfoBox.setVisible(true);
		box.add(fileGetInfoBox);
		// -----------操作消息--------------
		String friendQQ = message.getSrcQQ();
		String owerQQ = message.getDestQQ();
		FileResponseMessage responseMessage = new FileResponseMessage(owerQQ, friendQQ);
		responseMessage.setFileLength(message.getFileLength());
		responseMessage.setFileAbsolutePath(message.getFileAbsolutePath());
		responseMessage.setFileName(message.getFileName());

		responseMessage.setFileResponse(MessageType.FileResponse_Accepted);
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fileChooser.showOpenDialog(this);
		File file = fileChooser.getCurrentDirectory();
		String saveFilePatn = file.getAbsolutePath() + "\\" + message.getFileName();
		responseMessage.setSaveFilePath(saveFilePatn);
		EIMLogger.Error("此用户保存的路劲为：" + saveFilePatn);
		ClientFileSocket fileSocket = ClientFileSocket.getInstance();
		try {
			fileSocket.startSocket();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void refuseAction() {
		
		this.setVisible(false);
		container.removePanle(this);
	}

	private Box getFileInfoBox(String fileName) {
		Box box = Box.createHorizontalBox();
		
		String ext = ResourcesManagement.getFileExtractor(fileName);
		Icon icon = ResourcesManagement.getIconByExtractor(ext, true);
		box.add(new JCLabel(icon), BorderLayout.WEST);
		box.add(new JCLabel("    "));
		
		Box box2 = Box.createVerticalBox();
		box2.add(new JCLabel("文件请求"));
		box2.add(new JCLabel(fileName), BorderLayout.WEST);
		
		box.add(box2, BorderLayout.EAST);
		
		return box;
	}

	public void setProcess(int currentPackage, int totalPaclage) {
		
		double totalPaclageSize = totalPaclage;
		
		Double currentPrecent = currentPackage / totalPaclageSize * 100;
				
		fileTransProcess.setValue(currentPrecent.intValue());
		changeSpend(currentPackage, totalPaclage);
	}
	
	private long lastTime = new Date().getTime();
	private int lastPackageIndex = 0;
	private void changeSpend(int currentPackageIndex, int totalPaclageSize) {
		long currentTime = new Date().getTime();
		if(currentTime - lastTime >= 1000) {// 1 seconds
			lastTime = currentTime;
			int packageCount = currentPackageIndex-lastPackageIndex;// 这段时间内传过几个包，一个包大小为 1029 byte
			lblSpend.setText("当前速度："+packageCount+"kb/s");
			lastPackageIndex = currentPackageIndex;
		}
	}

	private Box getFileGetInfoBox() {
		Box box = Box.createVerticalBox();
		fileTransProcess = new JProgressBar();
		fileTransProcess.setBackground(Color.white);
		fileTransProcess.setForeground(Color.black);
		box.add(fileTransProcess);
		ompletionLabel = new JLabel("完成 :0%");
		speedLabel = new JLabel("速度: 350k/s");
		Box box4 = Box.createHorizontalBox();
		box4.add(ompletionLabel, BorderLayout.WEST);
		box4.add(new JLabel("    "));
		box4.add(refusebutton);
		box4.add(new JLabel("    "));
		box4.add(speedLabel, BorderLayout.EAST);
		box.add(box4);
		return box;
	}

	private Box getUserAction(boolean isGet) {
		Box box = Box.createHorizontalBox();
		if (isGet) {
			saveButton = new JLabel("<html><a href=\" \">另存为</a></html>");
			refusebutton = new JLabel("<html><a href=\" \">拒绝</a></html>");
			box.add(saveButton, BorderLayout.WEST);
			box.add(refusebutton, BorderLayout.EAST);
		} else {
			saveButton = new JLabel("<html><a href=\" \">发送离线文件</a></html>");
			refusebutton = new JLabel("<html><a href=\" \">取消</a></html>");
			box.add(saveButton, BorderLayout.WEST);
			box.add(new JLabel("      "));
			box.add(new JLabel("      "));
			box.add(refusebutton, BorderLayout.EAST);
		}
		return box;
	}

	public void setContainer(SendGetFilePanle container) {
		this.container = container;
	}

}